﻿using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;

namespace Accumulation.Database {
    /// <summary>
    /// 数据库参数准备类，如果创建的参数要指定具体的 DbType，在返回后指定
    /// （因为 DbType 是 Enum 类型，不能继承也不能重载）。
    /// 例：DbParams＜SqlParameter> p = new DbParams＜SqlParameter＞();
    /// p.Add＜string＞("@abc", "a").SqlDbType = SqlDbType.VarChar;
    /// <remarks>
    /// MatchZhou - 2009/3/26
    /// </remarks>
    /// </summary>
    /// <typeparam name="TParam">数据库参数类型，例：OleDbParameter</typeparam>
    public class DbParams<TParam> : CollectionBase
        where TParam : DbParameter, new() {

        private List<TParam> list = new List<TParam>();
        private Hashtable hashTable = new Hashtable();
        // 加入参数到 list 和 hashTable 中
        private void Add(TParam param) {
            list.Add(param);
            hashTable.Add(param.ParameterName, param);
        }
        /// <summary>
        /// 返回参数数组
        /// </summary>
        public TParam[] ToArray {
            get { return list.ToArray(); }
        }
        /// <summary>
        /// 通过索引序号查询参数
        /// </summary>
        /// <param name="index">序号索引</param>
        /// <returns>TParam</returns>
        public TParam this[int index] {
            set { list[index] = value; }
            get { return list[index]; }
        }
        /// <summary>
        /// 通过索引名称查询参数
        /// </summary>
        /// <param name="name">索引名称</param>
        /// <returns>TParam</returns>
        public TParam this[string name] {
            set { hashTable[name] = value; }
            get { return (TParam)hashTable[name]; }
        }
        /// <summary>
        /// 添加参数
        /// </summary>
        /// <param name="name">参数名称</param>
        /// <returns>参数</returns>
        public TParam Add(string name) {
            TParam param = new TParam();
            param.ParameterName = name;
            this.Add(param);
            return param;
        }
        /// <summary>
        /// 添加参数
        /// </summary>
        /// <typeparam name="D">数据值类型</typeparam>
        /// <param name="name">参数名称</param>
        /// <param name="value">数据值</param>
        /// <returns>参数</returns>
        public TParam Add<D>(string name, D value) {
            TParam param = new TParam();
            param.ParameterName = name;
            param.Value = value;
            this.Add(param);
            return param;
        }
        /// <summary>
        /// 添加参数
        /// </summary>
        /// <typeparam name="D">数据值类型</typeparam>
        /// <param name="name">参数名称</param>
        /// <param name="direction">数据方向</param>
        /// <param name="value">数据值</param>
        /// <returns>参数</returns>
        public TParam Add<D>(string name, ParameterDirection direction, D value) {
            TParam param = new TParam();
            param.ParameterName = name;
            param.Direction = direction;
            param.Value = value;
            this.Add(param);
            return param;
        }
        /// <summary>
        /// 添加参数
        /// </summary>
        /// <typeparam name="D">数据值类型</typeparam>
        /// <param name="name">参数名称</param>
        /// <param name="size">数据长度</param>
        /// <param name="value">数据值</param>
        /// <returns>参数</returns>
        public TParam Add<D>(string name, int size, D value) {
            TParam param = new TParam();
            param.ParameterName = name;
            param.Size = size;
            param.Value = value;
            this.Add(param);
            return param;
        }
        /// <summary>
        /// 添加参数
        /// </summary>
        /// <typeparam name="D">数据值类型</typeparam>
        /// <param name="name">参数名称</param>
        /// <param name="size">数据长度</param>
        /// <param name="direction">数据方向</param>
        /// <param name="value">数据值</param>
        /// <returns>参数</returns>
        public TParam Add<D>(string name, int size, ParameterDirection direction,D value) {
            TParam param = new TParam();
            param.ParameterName = name;
            param.Size = size;
            param.Direction = direction;
            param.Value = value;
            this.Add(param);
            return param;
        }
        /// <summary>
        /// 创建参数
        /// </summary>
        /// <typeparam name="D">数据值类型</typeparam>
        /// <param name="name">参数名称</param>
        /// <param name="value">数据值</param>
        /// <returns>参数</returns>
        public static TParam CreateParameter<D>(string name, D value) {
            TParam param = new TParam();
            param.ParameterName = name;
            param.Value = value;
            return param;
        }
        /// <summary>
        /// 创建参数
        /// </summary>
        /// <typeparam name="D">数据值类型</typeparam>
        /// <param name="name">参数名称</param>
        /// <param name="direction">数据方向</param>
        /// <param name="value">数据值</param>
        /// <returns>参数</returns>
        public static TParam CreateParameter<D>(string name, ParameterDirection direction, D value) {
            TParam param = new TParam();
            param.ParameterName = name;
            param.Direction = direction;
            param.Value = value;
            return param;
        }
        /// <summary>
        /// 创建参数
        /// </summary>
        /// <typeparam name="D">数据值类型</typeparam>
        /// <param name="name">参数名称</param>
        /// <param name="size">数据长度</param>
        /// <param name="value">数据值</param>
        /// <returns>参数</returns>
        public static TParam CreateParameter<D>(string name, int size, D value) {
            TParam param = new TParam();
            param.ParameterName = name;
            param.Size = size;
            param.Value = value;
            return param;
        }
        /// <summary>
        /// 创建参数
        /// </summary>
        /// <typeparam name="D">数据值类型</typeparam>
        /// <param name="name">参数名称</param>
        /// <param name="size">数据长度</param>
        /// <param name="direction">数据方向</param>
        /// <param name="value">数据值</param>
        /// <returns>参数</returns>
        public static TParam CreateParameter<D>(string name, int size, ParameterDirection direction, D value) {
            TParam param = new TParam();
            param.ParameterName = name;
            param.Size = size;
            param.Direction = direction;
            param.Value = value;
            return param;
        }
    }
}
